if (!window.openDatabase) {
	alert("Browser does NOT support Web SQL Database");
}
// OPEN
var db = openDatabase('webdb-database', '1.0', 'webdb', 5 * 1024 * 1024, null);

// CREATE TABLE
db.transaction(function(t) {
	t.executeSql('create table if not exists Names (Name TEXT, Timestamp INTEGER)',
			[], null, function(t, e) {
				console.error(e);
			});
});

// INSERT
db.transaction(function(t) {
	t.executeSql('insert into Names values (?, ?)',
			['Franek', new Date().getTime()], function(t, r) {
				var internalId = r.insertId;
				console.info('internalId = ' + internalId);
			}, function(t, e) {
				console.error(e);
			});
});

// SELECT
db.transaction(function(t) {
	t.executeSql('select Name, Timestamp from Names order by Timestamp desc', 
			[], function(t, r) {
				var result = '';
				for (var i = 0; i < r.rows.length; i++) {
					result += 'Name = ' + r.rows.item(i).Name + ', Timestamp = '
							+ r.rows.item(i).Timestamp + '\n';
				}
				console.info("result = " + result);
			}, function(t, e) {
				console.error(e);
			});
});

// UPDATE
db.transaction(function(t) {
	t.executeSql('update Names set Timestamp = ? where name = ?', 
			[new Date().getTime(), 'Franek'], null, function(t, e) {
				console.error(e);
			});
});

// SELECT
db.transaction(function(t) {
	t.executeSql('select Name, Timestamp from Names order by Timestamp desc', 
			[], function(t, r) {
				var result = '';
				for (var i = 0; i < r.rows.length; i++) {
					result += 'Name = ' + r.rows.item(i).Name + ', Timestamp = '
							+ r.rows.item(i).Timestamp + '\n';
				}
				console.info("result = " + result);
			}, function(t, e) {
				console.error(e);
			});
});

// DELETE
db.transaction(function(t) {
	t.executeSql('delete from Names where name = ?', ['Franek'], null, function(t, e) {
		console.error(e);
	});
});

//SELECT
db.transaction(function(t) {
	t.executeSql('select Name, Timestamp from Names order by Timestamp desc', 
			[], function(t, r) {
				var result = '';
				for (var i = 0; i < r.rows.length; i++) {
					result += 'Name = ' + r.rows.item(i).Name + ', Timestamp = '
							+ r.rows.item(i).Timestamp + '\n';
				}
				console.info("result = " + result);
			}, function(t, e) {
				console.error(e);
			});
});

// TRANSACTION
db.transaction(function(t) {
	t.executeSql('insert into Names values (?, ?)', ['Franek', new Date().getTime()]);
	t.executeSql('insert into Names values (?, ?)', ['Maciek', new Date().getTime()]);
	t.executeSql('insert into Names values (?, ?)', ['Iza', new Date().getTime()]);
	}, function (e) {
		console.error("error: " + e.message);
	}
);

//SELECT
db.transaction(function(t) {
	t.executeSql('select Name, Timestamp from Names order by Timestamp desc', 
			[], function(t, r) {
				var result = '';
				for (var i = 0; i < r.rows.length; i++) {
					result += 'Name = ' + r.rows.item(i).Name + ', Timestamp = '
							+ r.rows.item(i).Timestamp + '\n';
				}
				console.info("result = " + result);
			}, function(t, e) {
				console.error(e);
			});
});

// DROP
db.transaction(function(t) {
	t.executeSql('drop table Names', [], null, function(t, e) {
		console.error(e);
	});
});
